package com.heima.article.feign;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApAuthorService;
import com.heima.client.article.IArticleClient;
import com.heima.model.common.dtos.*;
import com.heima.model.common.pojos.article.ApArticle;
import com.heima.model.common.pojos.article.ApArticleConfig;
import com.heima.model.common.pojos.article.ApAuthor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 陈辉
 * @data 2024 16:12
 */
@RestController
@Slf4j
public class ArticleClient implements IArticleClient {
    @Autowired
    private ApArticleService apArticleService;

    @Autowired
    private ApAuthorMapper apAuthorMapper;



    @Autowired
    private ApAuthorService apAuthorService;

    /**
     * 保存或修改article文章信息
     *
     * @param articleDto
     * @return
     */
    @PostMapping("/api/v1/article/sync")
    public ResponseResult syncArticle(@RequestBody ArticleDto articleDto) {
        log.info("article文章同步开始了：{}....", articleDto);
       /* try {
            Thread.sleep(10000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return apArticleService.saveOrUpdateArticle(articleDto);
    }

    @PostMapping("api/v1/article/updateomment")
    public ResponseResult updateomment(@RequestBody CommentStatus commentStatus) {
        return apArticleService.CommentStatus(commentStatus);
    }

    @PostMapping("api/v1/article/articlesele")
    public ResponseResult articlesele(@RequestBody List<Long> entryIds) {
        return apArticleService.articlesele(entryIds);
    }

    @PostMapping("api/v1/article/getarticle/{userId}/{end}/{begin}")
    public List<ApArticle> getArticle(@PathVariable Integer userId, @PathVariable Date end, @PathVariable Date begin) {
        return apArticleService.getArticle(userId, end, begin);
    }

    @PostMapping("api/v1/article/iscomment/{id}")
    public ApArticleConfig getConfig(@PathVariable Long id) {
        return apArticleService.getConfig(id);
    }

    /**
     * 更新article内容
     *
     * @param behaviorDto
     * @return
     */
    @PostMapping("/api/v1/article/updateArticle")
    public ResponseResult updateArticle(@RequestBody ReadBehaviorDto behaviorDto) {
        log.info("接收到修改文章阅读次数的数据：{}", behaviorDto);
        return apArticleService.updateArticle(behaviorDto);
    }

    /**
     * 查询article文章信息
     *
     * @param articleDto
     * @return
     */
    @Override
    @PostMapping("/api/v1/article/loadArticle")
    public ResponseResult loadArticle(@RequestBody ArticleDto articleDto) {
        return apArticleService.findId(articleDto);
    }


    /**
     * 根据用户id查询用户信息
     *
     * @param userId
     * @return
     */
    @PostMapping("/api/v1/article/queryByUserId/{userId}")
    public ResponseResult queryByUserId(@PathVariable("userId")Integer userId) {
        //1、日志
        log.info("根据用户id查询用户信息：{}",userId);

        //2、调用Mapper进行查询并返回
        ApAuthor apAuthor = apAuthorMapper.selectById(userId);

        //3、返回结果
        return ResponseResult.okResult(apAuthor);
    }

    /**
     * 根据作者id查询作者信息
     *
     * @param authorId
     * @return
     */
    @Override
    @GetMapping("/api/v1/article/getUserInfo")
    public ResponseResult getUserInfo(@RequestParam Integer authorId) {

        return apAuthorService.getUserInfo(authorId);
    }

    /**
     * 查询文章列表
     *
     * @return
     */
    @GetMapping("/api/v1/article/getArticleList")
    public ResponseResult getArticleList(@RequestParam(value = "beginDate", required = false) String beginDate,
                                         @RequestParam(value = "endDate", required = false) String endDate,
                                         @RequestParam("userId") Integer userId) {
        return apArticleService.getArticleList(beginDate, endDate, userId);
    }

    @GetMapping("/api/v1/article/getPage")
    public ResponseResult getPage(@RequestParam(value = "beginDate", required = false) String beginDate,
                                  @RequestParam(value = "endDate", required = false) String endDate,
                                  @RequestParam(value = "page", required = false) Integer page,
                                  @RequestParam(value = "size", required = false) Integer size,
                                  @RequestParam("userId") Integer userId) {
        return apArticleService.getPage(beginDate, endDate, page, size, userId);
    }

    /**
     * 更新文章的状态
     * @param commentStatus
     * @return
     */
    @PostMapping("/api/v1/article/updateStatus")
    public ResponseResult updateStatus(@RequestBody CommentStatus commentStatus){
        return apArticleService.updateStatus(commentStatus);
    }

    /**
     * 更新文章的收藏状态 文章被收藏的次数
     * @param behaviorDto
     * @return
     */
    @PostMapping("/api/v1/article/updateCollection")
    public ResponseResult updateCollection(@RequestBody CollectionBehaviorDto behaviorDto){
        return apArticleService.updateCollection(behaviorDto);
    }

    /**
     * 查询文章列表
     * @param userId
     * @param end
     * @param begin
     * @param findNewsDto
     * @return
     */
    @PostMapping("api/v1/article/getarticle2/{userId}/{end}/{begin}")
    public Page<ApArticle> getArticle2(@PathVariable Integer userId, @PathVariable Date end, @PathVariable Date begin, @RequestBody FindNewsDto findNewsDto){
        return apArticleService.getArticle2(userId,end,begin,findNewsDto);
    }


   /* @PostMapping("/api/v1/article/updateCommentCount")
    public void updateCommentCount(@PathVariable("articleId")Long articleId) {

        return apArticleService.updateCommentCount(articleId);
    }*/
}
